/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package brickroadfitness;


import java.util.*;
import javax.swing.*;
import java.sql.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;


public class AddMember extends JFrame implements ActionListener
{
    public PreparedStatement pstmt;
    public Statement stmt1, stmt2;
    public ResultSet rset;
    public String query1, query2;
    //private static int memIDnum = 123458;
    private JPanel add;
    private JLabel addMember;
    //private JTextField bal;
   // private JLabel ballabel;
    private JButton cancel;
    private JLabel contactlabel;
    private JTextField contacts;
  //  private JLabel datejoinedlabel;
  //  private JLabel djoined;
    private JTextField firstname;
    private JLabel firstnamelabel;
    private JRadioButton male, female;
    private JLabel genderlabel;
    private JLabel header;
  //  private JLabel idlabel;
    private JTextField lastname;
    private JLabel lastnamelabel;
   // private JLabel memID;
    private JSpinner memexp;
    private JLabel memexplabel;
  //  private JRadioButton member, notmember;
  //  private JLabel memstatlabel;
  //  private JTextField midinitial;
  //  private JLabel midlabel;
    private JButton save;
    private JTextField addr;
    private JLabel addrlabel;
    private JOptionPane savemsg;
    private JOptionPane cancelmsg;
    private ButtonGroup gender;

    public AddMember()
    {
        super("Brickroad Fitness - Add Member");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBackground(new Color(255, 255, 102));
        setResizable(false);
        setVisible(true);
        //setLocationRelativeTo(null);
        setForeground(new Color(255, 255, 102));
        initComponents();
    }

        private void initComponents() {

        header = new JLabel();
        add = new JPanel();
        addMember = new JLabel();
     //   idlabel = new JLabel();
        lastnamelabel = new JLabel();
        firstnamelabel = new JLabel();
     //   midlabel = new JLabel();
        addrlabel = new JLabel();
        genderlabel = new JLabel();
        //memstatlabel = new JLabel();
        memexplabel = new JLabel();
        contactlabel = new JLabel();
      //  datejoinedlabel = new JLabel();
        //ballabel = new JLabel();
      //  memID = new JLabel();
        lastname = new JTextField();
        firstname = new JTextField();
      //  midinitial = new JTextField();
        addr = new JTextField();
        male = new JRadioButton();
        female = new JRadioButton();
        //member = new JRadioButton();
        //notmember = new JRadioButton();
        memexp = new JSpinner();
        contacts = new JTextField();
       // djoined = new JLabel();
        //bal = new JTextField();
        save = new JButton();
        cancel = new JButton();
        savemsg = new JOptionPane();
        cancelmsg = new JOptionPane();
        gender = new ButtonGroup();
        //memstat = new ButtonGroup();

        gender.add(male);
        gender.add(female);


        //memstat.add(member);
        //memstat.add(notmember);

        save.addActionListener(this);
        cancel.addActionListener(this);

        header.setIcon(new ImageIcon(getClass().getResource("/PRIMER.jpg"))); // NOI18N
        header.setText("Brickroad Fitness");

        add.setBackground(new Color(255, 255, 102));
        add.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0), 5));

        addMember.setFont(new Font("Arial", 3, 18)); // NOI18N
        addMember.setText("Add Member");

      //  idlabel.setFont(new Font("Tempus Sans ITC", 1, 14)); // NOI18N
      //  idlabel.setText("Membership ID:");

        lastnamelabel.setFont(new Font("Tempus Sans ITC", 1, 14)); // NOI18N
        lastnamelabel.setText("Last Name:");

        firstnamelabel.setFont(new Font("Tempus Sans ITC", 1, 14)); // NOI18N
        firstnamelabel.setText("First Name:");

      //  midlabel.setFont(new Font("Tempus Sans ITC", 1, 14)); // NOI18N
      //  midlabel.setText("Middle Name:");

        addrlabel.setFont(new Font("Tempus Sans ITC", 1, 14)); // NOI18N
        addrlabel.setText("Address:");

        genderlabel.setFont(new Font("Tempus Sans ITC", 1, 14)); // NOI18N
        genderlabel.setText("Gender:");
/*
        Date initDate = calendar.getTime();
        calendar.add(Calendar.YEAR, -100);
        Date earliestDate = calendar.getTime();
        calendar.add(Calendar.YEAR, 200);
        Date latestDate = calendar.getTime();
        model = new SpinnerDateModel(initDate,
                             earliestDate,
                             latestDate,
                             Calendar.YEAR);*/
        memexp.setModel(new SpinnerDateModel());
        memexp.setEditor(new JSpinner.DateEditor(memexp, "yyyy-MM-dd"));

        //memstatlabel.setFont(new Font("Tempus Sans ITC", 1, 14)); // NOI18N
        //memstatlabel.setText("Membership Status:");

        memexplabel.setFont(new Font("Tempus Sans ITC", 1, 14)); // NOI18N
        memexplabel.setText("Membership Expiration:");

        contactlabel.setFont(new Font("Tempus Sans ITC", 1, 14)); // NOI18N
        contactlabel.setText("Contact Details:");

      //  datejoinedlabel.setFont(new Font("Tempus Sans ITC", 1, 14)); // NOI18N
      //  datejoinedlabel.setText("Date Joined:");

       // ballabel.setFont(new Font("Tempus Sans ITC", 1, 14)); // NOI18N
       // ballabel.setText("Balance:");

      //  memID.setFont(new Font("Tahoma", 1, 12)); // NOI18N
     //   memID.setText(Integer.toString(memIDnum));

        lastname.setFont(new Font("Tahoma", 1, 12)); // NOI18N

        firstname.setFont(new Font("Tahoma", 1, 12)); // NOI18N

      //  midinitial.setFont(new Font("Tahoma", 1, 12)); // NOI18N

        addr.setFont(new Font("Tahoma", 1, 12)); // NOI18N

        male.setBackground(new Color(255, 255, 102));
        male.setFont(new Font("Tahoma", 1, 14)); // NOI18N
        male.setSelected(true);
        male.setText("Male");

        female.setBackground(new Color(255, 255, 102));
        female.setFont(new Font("Tahoma", 1, 14)); // NOI18N
        female.setText("Female");

       // member.setBackground(new Color(255, 255, 102));
       // member.setFont(new Font("Tahoma", 1, 14)); // NOI18N
       // member.setSelected(true);
       // member.setText("Member");

       // notmember.setBackground(new Color(255, 255, 102));
       // notmember.setFont(new Font("Tahoma", 1, 14)); // NOI18N
       // notmember.setText("Not a member");

        memexp.setFont(new Font("Tahoma", 1, 12)); // NOI18N

        contacts.setFont(new Font("Tahoma", 1, 12)); // NOI18N

      //  djoined.setFont(new Font("Tahoma", 1, 12)); // NOI18N
      //  djoined.setText(now("dd MMMMM yyyy"));

       // bal.setFont(new Font("Tahoma", 1, 12)); // NOI18N

        save.setFont(new Font("Century Gothic", 1, 18)); // NOI18N
        save.setText("SAVE");
        save.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        cancel.setFont(new Font("Century Gothic", 1, 18)); // NOI18N
        cancel.setText("CANCEL");
        cancel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        GroupLayout addLayout = new GroupLayout(add);
        add.setLayout(addLayout);
        addLayout.setHorizontalGroup(
            addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(addLayout.createSequentialGroup()
                .addGap(75, 75, 75)
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    //.addComponent(idlabel)
                    .addComponent(lastnamelabel)
                    .addComponent(firstnamelabel)
                    //.addComponent(midlabel)
                    .addComponent(addrlabel)
                    .addComponent(genderlabel)
                    //.addComponent(memstatlabel)
                    .addComponent(memexplabel)
                    .addComponent(contactlabel))
                    //.addComponent(datejoinedlabel))
                    //.addComponent(ballabel))
                .addGap(29, 29, 29)
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                   // .addComponent(memID, GroupLayout.DEFAULT_SIZE, 433, Short.MAX_VALUE)
                    .addComponent(lastname)
                    .addComponent(firstname)
                   // .addComponent(midinitial, GroupLayout.Alignment.TRAILING)
                    .addComponent(addr)
                    .addComponent(memexp)
                    .addComponent(contacts)
                    //.addComponent(djoined)
                    //.addComponent(bal)
                .addGroup(addLayout.createSequentialGroup()
                     .addGroup(addLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                          .addComponent(male))
                         // .addComponent(member))
                     .addGap(46, 46, 46)
                     .addGroup(addLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                          //.addComponent(notmember)
                          .addComponent(female))))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 73, Short.MAX_VALUE)
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                    .addComponent(cancel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(save, GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE))
                .addGap(32, 32, 32))
            .addGroup(addLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(addMember)
                .addContainerGap(767, Short.MAX_VALUE))
        );
        addLayout.setVerticalGroup(
            addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(addLayout.createSequentialGroup()
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(addLayout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(addMember))
                        /*.addGap(18, 18, 18)
                        .addComponent(idlabel)
                    .addGroup(addLayout.createSequentialGroup()
                        .addGap(58, 58, 58)
                        .addComponent(memID, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))*/)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(lastnamelabel)
                    .addComponent(lastname, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(firstnamelabel)
                    .addComponent(firstname, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
               /* .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(midlabel)
                    .addComponent(midinitial, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))*/
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(addrlabel)
                    .addComponent(addr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(genderlabel)
                    .addComponent(male)
                    .addComponent(female))
                /*.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(memstatlabel)
                    .addComponent(member)
                    .addComponent(notmember))*/
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(addLayout.createSequentialGroup()
                        .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(memexplabel)
                            .addComponent(memexp, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(contactlabel)
                            .addComponent(contacts, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        /*.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(datejoinedlabel)
                            .addComponent(djoined, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))*/
                        /*.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(addLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(ballabel)
                            .addComponent(bal, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))*/)
                    .addGroup(addLayout.createSequentialGroup()
                        .addComponent(save, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cancel, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(32, Short.MAX_VALUE))
        );

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(header, GroupLayout.PREFERRED_SIZE, 919, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(add, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(header, GroupLayout.PREFERRED_SIZE, 198, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(add, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }

    public void actionPerformed(ActionEvent e)
    {
        Object event = new Object();
        event = e.getSource();
        cnd a=new cnd();
        int n = 0;
        int b = 1;
        int idnumber=0;
        String genderbox;

        if (event == save)
        {
            if( !(lastname.getText().isEmpty())
                    && !(firstname.getText().isEmpty())
                    && !(addr.getText().isEmpty()) && !(gender.getSelection() == null)
                    && !(memexp.getValue() == null)
                    && !(contacts.getText().isEmpty()))
            {
                int ans;
                ans = savemsg.showConfirmDialog(this, "Are you sure you want to add a new member?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
                if(male.isSelected()){
                genderbox="Male";
                }
                else{
                genderbox="Female";
                }
                if (ans == 0)
                {
                    a.connect();
                    try {
                        long currentTime = System.currentTimeMillis();
                        java.sql.Date date = new java.sql.Date(currentTime - (currentTime % 86400000));
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        stmt2 = a.con2.createStatement();
                        //while (rset.next())
                       //{
                             n++;      
                             query1= "SELECT MAX(mID) FROM `member`";
                             stmt1 = a.con1.createStatement();
                             rset = stmt1.executeQuery(query1);
                             rset.next();
                             System.out.println(idnumber);
                             //if (!(rset.next()))
                             idnumber= rset.getInt("max(mID)");
                             //else
                             //    idnumber = 0;
                             //idnumber=Integer.parseInt(temp);
                             idnumber=idnumber+1;
                             System.out.println(idnumber);
                             query2 = "INSERT INTO `Member`(`mID`, `mFirstname`, `mLastname`, `mAddress`,`mGender`, `mContact`, `mDateJoined`, `mLastCheckin`, `mExpiration`) " +
                                           "VALUES('" + idnumber + "', '" +
                                                              firstname.getText() + "', '" + lastname.getText() + "', '" + addr.getText()+"', '" + genderbox+"', '" + contacts.getText() + "', '"+ date + "', '" +date+
                                                              "', '" + sdf.format(memexp.getValue()) +"')";

                             
                             stmt2.executeUpdate(query2);

                       //}
                        System.out.println(n + " records from member table were successfully copied.");
                        savemsg.showMessageDialog(this, "Save successful!", "Save", JOptionPane.INFORMATION_MESSAGE);
                        a.disconnect();
                        dispose();
                        } catch (Exception ex) {
                        System.out.println("Error copying records from member table. " + ex.getMessage());
                        }
                }
            }

            else
                savemsg.showMessageDialog(this, "Incomplete fields", "Error", JOptionPane.ERROR_MESSAGE);
        }
        if (event == cancel)
        {
            int ans;
            ans = cancelmsg.showConfirmDialog(this, "Are you sure you want to cancel? All unsaved information will be lost.", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
            if(ans == 0)
            {
                dispose();
            }
        }

    }

     public static String now(String dateformat)
     {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(dateformat);
        return sdf.format(cal.getTime());

     }
}